//
//  BanquetHallDetailViewController.swift
//  FeastBooking
//
//  Created by Imon on 2017/9/11.
//  Copyright © 2017年 Imon. All rights reserved.
//

import UIKit
import Kingfisher
import RxSwift

class BanquetHallDetailViewController: UIViewController {
    
    let authManager = AuthManager.share
    
    //let userAuthVM = UserAuthViewModel.shared
    
    let disposeBag = DisposeBag()
    
    var banquetHall: BanquetHallDetail?
    
    var restaurant: RestaurantDetailResponse?
    
    var scrollView: UIScrollView?
    
    @IBOutlet weak var button: UIButton!
    
    var imageScrollView: UIScrollView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = banquetHall?.name
        
        button.layer.cornerRadius = 3
        button.layer.masksToBounds = true
        
        setBackBarItem()
        
        scrollView = UIScrollView()
        scrollView?.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height - 50)
        scrollView?.showsVerticalScrollIndicator = false
        scrollView?.showsHorizontalScrollIndicator = false
        scrollView?.isScrollEnabled = true
        scrollView?.alwaysBounceVertical = true
        scrollView?.autoresizingMask = UIViewAutoresizing.flexibleHeight
        scrollView?.backgroundColor = UIColor.white
        //        scrollView?.delegate = self
        
        imageScrollView = UIScrollView()
        let width = self.view.frame.width
        imageScrollView?.frame = CGRect(x: 0, y: 0, width: width, height: width * 9 / 16)
        imageScrollView?.isPagingEnabled = true
        imageScrollView?.isScrollEnabled = true
        imageScrollView?.showsVerticalScrollIndicator = false
        imageScrollView?.showsHorizontalScrollIndicator = false
        imageScrollView?.scrollsToTop = false
        var contentCount = 1
        if let urls = banquetHall?.pictureUrls {
            if urls.count > 0 {
                contentCount = urls.count
                for i in 0...contentCount - 1 {
                    let imageView = UIImageView()
                    imageView.contentMode = .scaleAspectFill
                    imageView.layer.masksToBounds = true
                    imageView.frame = CGRect(x: CGFloat(i) * (imageScrollView?.frame.size.width)!, y: 0, width: (imageScrollView?.frame.size.width)!, height: (imageScrollView?.frame.size.height)!)
                    let url = URL(string: urls[i])
                    imageView.kf.setImage(with: url, placeholder: UIImage(named: "loading"))
                    imageScrollView?.addSubview(imageView)
                }
            } else {
                setDefaultImage()
            }
        } else {
            setDefaultImage()
        }
        imageScrollView?.contentSize = CGSize(width: (imageScrollView?.frame.width)! * CGFloat(contentCount), height: (imageScrollView?.frame.height)!)
        //        pageControl.numberOfPages = contentCount
        //        pageControl.alpha = 0
        //        pageControl.currentPage = 0
        scrollView?.addSubview(imageScrollView!)
        
        let iconSize: CGFloat = 16
        let textMaxSize = CGSize(width: self.view.bounds.width, height: 20)
        var currentY: CGFloat = 0
        
        let environmentIcon = UIImageView()
        environmentIcon.image = UIImage(named: "list2")
        environmentIcon.frame = CGRect(x: 15, y: (imageScrollView?.frame.origin.y)! + (imageScrollView?.frame.height)! + 15, width: iconSize, height: iconSize)
        
        let environmentLabel = UILabel()
        environmentLabel.font = AppFont(size: 14)
        environmentLabel.textColor = UIColor.gray
        environmentLabel.text = LanguageHelper.getString(key: "ENV")
        let enLabelSize = TextSize.textSize(text: environmentLabel.text!, font: environmentLabel.font, maxSize: textMaxSize)
        environmentLabel.frame = CGRect(x: 15 + iconSize + 15, y: (imageScrollView?.frame.origin.y)! + (imageScrollView?.frame.height)! + 15, width: enLabelSize.width, height: enLabelSize.height)
        currentY = environmentLabel.frame.origin.y + environmentLabel.frame.height
        
        let labelLeading: CGFloat = 30
        let gap: CGFloat = 15
        let labelWidth: CGFloat = (self.view.bounds.width - CGFloat(2) * labelLeading - gap) / 2
        let labelHeight: CGFloat = 15
        
        let areaLabel = UILabel()
        areaLabel.font = AppFont(size: 12)
        areaLabel.textColor = UIColor.lightGray
        areaLabel.text = "\(LanguageHelper.getString(key: "AREA"))：\((banquetHall?.area)!)㎡"
        areaLabel.frame = CGRect(x: labelLeading, y: currentY + 20, width: labelWidth, height: labelHeight)
        
        let heightLabel = UILabel()
        heightLabel.font = AppFont(size: 12)
        heightLabel.textColor = UIColor.lightGray
        heightLabel.text = "\(LanguageHelper.getString(key: "HEIGHT"))：\((banquetHall?.height)!)m"
        heightLabel.frame = CGRect(x: labelLeading + labelWidth + gap, y: currentY + 20, width: labelWidth, height: labelHeight)
        
        let pillarLabel = UILabel()
        pillarLabel.font = AppFont(size: 12)
        pillarLabel.textColor = UIColor.lightGray
        pillarLabel.text = "\(LanguageHelper.getString(key: "PILLAR"))：\((banquetHall?.columns)!)"
        pillarLabel.frame = CGRect(x: labelLeading, y: currentY + 20 + (labelHeight + gap), width: labelWidth, height: labelHeight)
        
        let shapeLabel = UILabel()
        shapeLabel.font = AppFont(size: 12)
        shapeLabel.textColor = UIColor.lightGray
        shapeLabel.text = "\(LanguageHelper.getString(key: "SHAPE"))：\((banquetHall?.shape)!)"
        shapeLabel.frame = CGRect(x: labelLeading + labelWidth + gap, y: currentY + 20 + (labelHeight + gap), width: labelWidth, height: labelHeight)
        
        let receptionLabel = UILabel()
        receptionLabel.font = AppFont(size: 12)
        receptionLabel.textColor = UIColor.lightGray
        receptionLabel.text = "\(LanguageHelper.getString(key: "RECEPTION_AREA"))：\((banquetHall?.actualArea)!)"
        receptionLabel.frame = CGRect(x: labelLeading, y: currentY + 20 + (labelHeight + gap) * 2, width: labelWidth, height: labelHeight)
        
        let colorLabel = UILabel()
        colorLabel.font = AppFont(size: 12)
        colorLabel.textColor = UIColor.lightGray
        colorLabel.text = "\(LanguageHelper.getString(key: "TABLE_CLOTH_COLOR"))：\((banquetHall?.colorOfTablecloth)!)"
        colorLabel.frame = CGRect(x: labelLeading + labelWidth + gap, y: currentY + 20 + (labelHeight + gap) * 2, width: labelWidth, height: labelHeight)
        
        let tablesLabel = UILabel()
        tablesLabel.font = AppFont(size: 12)
        tablesLabel.textColor = UIColor.lightGray
        if let tableRange = banquetHall?.tableRange {
            tablesLabel.text = "\(LanguageHelper.getString(key: "AVAILABLE_TABLE"))：\(tableRange[0])-\(tableRange[1])\(LanguageHelper.getString(key: "TABLE"))"
        } else {
            tablesLabel.text = "\(LanguageHelper.getString(key: "AVAILABLE_TABLE"))："
        }
        tablesLabel.frame = CGRect(x: labelLeading, y: currentY + 20 + (labelHeight + gap) * 3, width: labelWidth, height: labelHeight)
        
        let consumptionLabel = UILabel()
        consumptionLabel.font = AppFont(size: 12)
        consumptionLabel.textColor = UIColor.lightGray
        consumptionLabel.text = "\(LanguageHelper.getString(key: "MIN_CONSUMPTION"))：$\((banquetHall?.minimumPrice)!)"
        consumptionLabel.frame = CGRect(x: labelLeading + labelWidth + gap, y: currentY + 20 + (labelHeight + gap) * 3, width: labelWidth, height: labelHeight)
        
        currentY = consumptionLabel.frame.origin.y + consumptionLabel.frame.height
        
        let saperator = UIView()
        saperator.backgroundColor = saperatorColor
        saperator.frame = CGRect(x: 30, y: currentY + 30, width: self.view.bounds.width - 60, height: 0.5)
        
        currentY = saperator.frame.origin.y + saperator.frame.height
        
        let configurationIcon = UIImageView()
        configurationIcon.image = UIImage(named: "list2")
        configurationIcon.frame = CGRect(x: 15, y: currentY + 30, width: iconSize, height: iconSize)
        
        let configurationLabel = UILabel()
        configurationLabel.font = AppFont(size: 14)
        configurationLabel.textColor = UIColor.gray
        configurationLabel.text = "\(LanguageHelper.getString(key: "CONFIGURATION"))"
        let conLabelSize = TextSize.textSize(text: configurationLabel.text!, font: configurationLabel.font, maxSize: textMaxSize)
        configurationLabel.frame = CGRect(x: 15 + iconSize + 15, y: currentY + 30, width: conLabelSize.width, height: conLabelSize.height)
        currentY = configurationLabel.frame.origin.y + configurationLabel.frame.height
        
        if let configs = banquetHall?.extraInfos {
            if configs.count > 0 {
                let cLeading: CGFloat = 15
                let cViewWidth: CGFloat = (self.view.bounds.width - CGFloat(2) * cLeading) / 3
                let cViewHeight: CGFloat = 20
                let gap: CGFloat = 10
                for i in 0...configs.count - 1 {
                    let vOffset = i % 3
                    let hOffset = i / 3
                    let cView = BHConfigurationView()
                    cView.contentLabel.text = configs[i]
                    cView.frame = CGRect(x: cLeading + CGFloat(vOffset) * cViewWidth, y: currentY + 20 + (cViewHeight + gap) * CGFloat(hOffset), width: cViewWidth, height: cViewHeight)
                    scrollView?.addSubview(cView)
                    if i == (configs.count - 1) {
                        currentY = cView.frame.origin.y + cView.frame.height
                    }
                }
            }
        }
        
        scrollView?.addSubview(environmentIcon)
        scrollView?.addSubview(environmentLabel)
        scrollView?.addSubview(areaLabel)
        scrollView?.addSubview(heightLabel)
        scrollView?.addSubview(pillarLabel)
        scrollView?.addSubview(shapeLabel)
        scrollView?.addSubview(receptionLabel)
        scrollView?.addSubview(colorLabel)
        scrollView?.addSubview(tablesLabel)
        scrollView?.addSubview(consumptionLabel)
        scrollView?.addSubview(saperator)
        scrollView?.addSubview(configurationIcon)
        scrollView?.addSubview(configurationLabel)
        
        scrollView?.contentSize = CGSize(width: self.view.bounds.width, height: currentY + 20)
        
        self.view.addSubview(scrollView!)
    }
    
    @IBAction func consult(_ sender: UIButton) {
        if authManager.hasLogined() {
            showBookingView()
        } else {
            showLoginPage()
        }
    }
    
    private func showBookingView() {
        let bookingView = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "BookingViewController") as! BookingViewController
        bookingView.restaurant = self.restaurant
        bookingView.chosenBH = RestaurantBanquetHall(id: (self.banquetHall?.id)!, name: (self.banquetHall?.name)!)
        bookingView.chosenBH?.tableRange = self.banquetHall?.tableRange
        bookingView.isBH = true
        self.navigationController?.pushViewController(bookingView, animated: true)
    }
    
    func setDefaultImage() {
        //沒有附带照片
        let imageView = UIImageView(image: UIImage(named: "ph_long"))
        imageView.contentMode = .scaleAspectFill
        imageView.layer.masksToBounds = true
        imageView.frame = CGRect(x: 0, y: 0, width: (imageScrollView?.frame.size.width)!, height: (imageScrollView?.frame.size.height)!)
        imageScrollView?.addSubview(imageView)
    }
    
    
}
